<template>
    <div>
        <el-card>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
                <el-breadcrumb-item>轮播图列表</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="btnBox">
                <div>
                    <el-button size="small" @click="addBanner">添加</el-button>
                    <el-button size="small" icon="el-icon-upload2" @click="handle('up', 'top')">上线</el-button>
                    <el-button size="small" icon="el-icon-download" @click="handle('down', 'top')">下线</el-button>
                </div>
            </div>
            <el-table :data="tableData" border style="width: 100%" height="626" @selection-change="selectChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="bannerId" label="ID" width="55"></el-table-column>
                <el-table-column label="轮播图" width="150" size="small">
                    <template slot-scope="prop">
                        <el-image :src="prop.row.pictureAddress" class="image-control"
                            :previewSrcList="[prop.row.pictureAddress]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="pictureUrl" label="轮播图地址">
                    <template slot-scope="scope">
                        {{ scope.row.pictureUrl | getUrlName }}
                    </template>
                </el-table-column>
                <el-table-column prop="bandingName" label="绑定内容"></el-table-column>
                <el-table-column label="绑定类型">
                    <template slot-scope="scope">
                        {{ scope.row.flag | getTypeName }}
                    </template>
                </el-table-column>
                <el-table-column prop="startTime" label="开始时间"></el-table-column>
                <el-table-column prop="endTime" label="结束时间"></el-table-column>
                <el-table-column label="轮播图状态">
                    <template slot-scope="scope">
                        <el-tag size="small" v-if="scope.row.pictureStatus == '1'">已上线</el-tag>
                        <el-tag size="small" type="warning" v-else>已下线</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button size="small" icon="el-icon-edit" @click="editHandle(scope.row)">修改</el-button>
                        <el-button size="small" icon="el-icon-upload2" v-if="scope.row.pictureStatus == '0'"
                            @click="handle('up', 'table', scope.row)">上线</el-button>
                        <el-button size="small" icon="el-icon-download" @click="handle('down', 'table', scope.row)"
                            v-if="scope.row.pictureStatus == '1'">下线</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="paginationBox">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="pageNum" :page-sizes="[50, 100, 200]" :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper" :total="totalNum"></el-pagination>
            </div>
        </el-card>

        <!-- 添加弹窗 -->
        <el-dialog title="轮播图" :visible.sync="addialogVisible" width="30%" top="15vh" :before-close="closeSortDialog">
            <el-form :model="form" label-width="120px" :rules="rules" ref="form">

                <el-form-item label="链接内容" prop="flag">
                    <el-select v-model="form.flag" placeholder="请选择" @change="getBandingList">
                        <el-option v-for="item in linkList" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="上线时间" prop="startTime">
                    <el-date-picker v-model="form.startTime" type="date" placeholder="选择上线时间" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="下线时间" prop="endTime">
                    <el-date-picker v-model="form.endTime" type="date" placeholder="选择下线时间" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="关联内容" v-if="form.flag != -1 && form.flag != 3">
                    <el-select v-model="form.bandingId" placeholder="请选择关联内容">
                        <el-option v-for="item in bandingList" :key="item.bandingId" :label="item.bandingName"
                            :value="item.bandingId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="链接页面" v-if="form.flag == 3">
                    <el-select v-model="form.pictureUrl" placeholder="请选择链接页面">
                        <el-option v-for="item in pageUrlList" :key="item.id" :label="item.text" :value="item.url">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="轮播图">
                    <div class="uploadBox">
                        <div class="imgListBox" v-if="form.mainPicUrl != ''">
                            <div>
                                <el-image :src="form.mainPicUrl" class="avatar"
                                    :previewSrcList="[form.mainPicUrl]"></el-image>
                                <div class="closeBox" @click="delImg()">
                                    <i class="el-icon-circle-close"></i>
                                </div>
                            </div>
                        </div>
                        <el-upload v-else action="aa" class="avatar-uploader" :on-change="proMainChange"
                            :auto-upload="false" accept=".jpg,.png" ref="addProduct_pic" :show-file-list="false">
                            <i class="el-icon-plus avatar-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">*注：尺寸为750px*302px，只能上传jpg/png文件</div>
                        </el-upload>
                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeSortDialog">取 消</el-button>
                <el-button type="primary" @click="submitSortDialog">确 定</el-button>
            </span>
        </el-dialog>
    </div>

</template>


<script>
let that
import { backGetBanner, getBandingByType, backaddBanner, updateStatus, updateBanner } from '../../network/banner'

export default {
    data() {
        return {
            loading: false,
            bandingId: 0,
            bandingList: [],
            tableData: [],
            selectList: [],
            pageSize: 50,
            pageNum: 1,
            totalNum: 0,
            form: {
                pictureUrl: '',
                startTime: '',
                endTime: '',
                mainPic: '',
                mainPicUrl: '',
                flag: -1,
                bandingId: ''
            },
            addialogVisible: false,
            rules: {
                flag: [
                    { required: true, message: '请选择链接内容', trigger: 'change' },
                ],
                startTime: [
                    { required: true, message: '请选择上线时间', trigger: 'change' },
                ],
                endTime: [
                    { required: true, message: '请选择下线时间', trigger: 'change' },
                ],

            },

            linkList: [
                {
                    value: -1,
                    label: '无跳转'
                },
                {
                    value: 0,
                    label: '商品链接'
                },
                {
                    value: 1,
                    label: '科普文章'
                },
                {
                    value: 2,
                    label: '科普视频'
                },
                {
                    value: 3,
                    label: '链接页面'
                },
            ],

            pageUrlList: [
                {
                    id: 1,
                    text: '钻石商城',
                    url: '/mainPackage/pages/diamondMall/diamondMall',
                }
            ],
            editFlag: false

        };
    },
    methods: {

        selectChange(val) {
            this.selectList = val
        },
        getBandingList() {
            if (this.form.flag == 3) {
                return
            }
            getBandingByType({
                type: this.form.flag,
                name: ''
            }).then(res => {
                if (res.flag) {
                    this.bandingList = res.data
                    this.loading = false;
                }
            })
        },

        proMainChange(file) {
            let fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
            console.log(fileType);

            if (
                fileType == 'jpg' ||
                fileType == 'png'
            ) {
                this.form.mainPicUrl = URL.createObjectURL(file.raw);
                this.form.mainPic = file.raw;
            } else {
                // 提醒只能上传的文件类型
                this.$message.warning('只能上传jpg或png文件')
                return;
            }
            console.log(this.form);
        },
        delImg() {
            console.log(this.form);
            if (this.editFlag) {
                this.form.pictureAddress = ''
            }
            this.form.mainPicUrl = ''
            this.form.mainPic = ''

            this.$forceUpdate()
        },
        closeSortDialog() {
            this.addialogVisible = false
            this.form = {
                pictureUrl: '',
                startTime: '',
                endTime: '',
                mainPic: '',
                mainPicUrl: '',
                flag: -1,
                bandingId: ''
            }
        },
        submitSortDialog() {

            this.$refs.form.validate((valid) => {
                if (!valid) return
                var form = this.form;

                if (form.mainPic == '') {
                    this.$message.warning('请上传轮播图')
                    return
                }
                if (form.flag != -1 && form.flag != 3 && form.bandingId == '') {
                    this.$message.warning('请选择关联内容')

                    return
                }

                if (form.flag == 3 && form.pictureUrl == '') {
                    this.$message.warning('请选择链接页面')
                    return
                }

                console.log(form);

                if (!this.editFlag) {
                    backaddBanner(form).then(res => {
                        if (res.flag) {
                            console.log(res);
                            if (res.flag) {
                                this.$message.success('添加成功')
                                this.getList();
                                this.closeSortDialog()
                            }
                        }
                    })
                } else {

                    if (form.pictureAddress == '') {
                        this.$message.warning('请上传轮播图')
                        return
                    }


                    updateBanner(form).then(res => {
                        if (res.flag) {
                            console.log(res);
                            if (res.flag) {
                                this.$message.success('修改成功')
                                this.getList();
                                this.closeSortDialog()
                            }
                        }
                    })
                }


            });
        },
        getList() {
            backGetBanner({
                pageSize: this.pageSize,
                pageNum: this.pageNum
            }).then(res => {
                if (res.flag) {
                    this.tableData = res.data.list
                    this.totalNum = res.data.total
                }
            })
        },

        editHandle(obj) {
            this.form = JSON.parse(JSON.stringify(obj))
            console.log(obj);
            this.form.mainPicUrl = obj.pictureAddress
            console.log(this.form);
            this.editFlag = true
            this.getBandingList()
            this.addialogVisible = true
        },


        handle(flag, position, obj) {
            if (position == 'top' && this.selectList.length == 0) {
                this.$message.warning('请选择要操作的数据')
                return
            }
            if (position == 'top') {
                var ids = '';
                this.selectList.forEach(ele => {
                    ids += ele.bannerId + ','
                })
                updateStatus({
                    ids,
                    status: flag == 'up' ? 1 : 0,
                }).then(res => {
                    if (res.flag) {
                        this.$message.success('操作成功')
                        this.getList()
                    } else {
                        this.$message.warning('操作失败')
                    }
                })
            } else {
                var sendForm = {
                    ids: obj.bannerId,
                    status: '',
                }
                // 上架 下架
                sendForm.status = flag == 'up' ? 1 : 0
                sendForm.flag = 0
                updateStatus(sendForm).then(res => {
                    if (res.flag) {
                        this.$message.success('操作成功')
                        this.getList()
                    } else {
                        this.$message.warning('操作失败')
                    }
                })
            }
        },

        addBanner() {
            this.addialogVisible = true
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getList();
        },



    },
    created() {
        this.getList();
        that = this
    },
    filters: {
        getUrlName(val) {
            var name = '无绑定'
            that.pageUrlList.forEach(ele => {
                if (ele.url == val) {
                    name = ele.text
                }
            })
            return name
        },
        getTypeName(val) {
            var name = ''
            that.linkList.forEach(ele => {
                if (ele.value == val) {
                    name = ele.label
                }
            })
            return name
        },
    }
};

</script>

<style scoped>
.image-control {
    width: 120px;
    height: 120px;
}


.demo-ruleForm {
    margin-top: 16px;
    height: 78vh;
    overflow-y: scroll;
}

.demo-ruleForm::-webkit-scrollbar {
    display: none;
}

.el-input,
.el-select,
.el-cascader {
    width: 250px;
}


.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    border: 1px dashed #dcdfe6;
    border-radius: 8px;
}

.avatar {

    width: 100px;
    height: 100px;
    display: block;
}

.imgListBox,
.uploadBox {
    display: flex;
    align-content: center;
}

.closeBox {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0px;
    right: 0px;
    width: 20px;
    height: 20px;
    background-color: rgba(0, 0, 0, .5);
    color: #fff;
    border-radius: 4px;
    cursor: pointer;
    z-index: 10;
}

.playBtn {
    position: absolute;
    top: 34px;
    left: 34px;
    font-size: 32px;
    color: #fff;
}

.imgListBox>div {
    position: relative;
    margin-right: 16px;
}

.el-upload__tip {
    color: red;

}

.videoBox {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}


.videoCon {
    width: 500px;
    height: 500px;
}

.formatBox {
    display: flex;
    align-items: center;
    width: 100%;

}

.formatBox>div>.el-input {
    width: 150px;
}

.formatBox>div {
    margin: 16px 0 0 16px;
    display: flex;
    align-items: center;
}

.avatar-uploader1,
.avatar-uploader-icon1,
.avatar1 {
    width: 50px !important;
    height: 50px !important;
    line-height: 50px !important;
}

.addBtn {
    font-size: 18px;
    margin-left: 16px;
    color: #999;
    cursor: pointer;
}
</style>
